using Abp.Application.Services.Dto;
using Abp.Auditing;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.Runtime.Session;
using Mt.Site.Application.Dto.Add;
using Mt.Site.Application.SEO.Dto;
using Mt.Site.Core.SEO;
using System;
using System.Threading.Tasks;

namespace Mt.Site.Application.SEO
{
    public class SeoAppService : MtSiteAppServiceBase, ISeoAppService
    {
        private readonly IRepository<Seo, Guid> _seoRepository;

        private readonly ISeoFrontEndAppService _seoFrontEndAppService;

        public SeoAppService(
            IRepository<Seo, Guid> seoRepository,
            ISeoFrontEndAppService seoFrontEndAppService)
        {
            _seoRepository = seoRepository;
            _seoFrontEndAppService = seoFrontEndAppService;
        }

        [AbpAuthorize(SeoPermissions.Pages_Manange_Tenant_Seo)]
        public async Task CreateOrUpdateSeoSetting(CreateOrUpdateSeoInput input)
        {
            using (CurrentUnitOfWork.SetCultureName(AbpSession.CultureName()))
            {
                Seo entity;
                entity = await _seoRepository.FirstOrDefaultAsync(s => s.RelatedId == input.RelatedId);
                if (entity != null)
                {
                    entity.RelatedId = input.RelatedId;
                    entity.Title = input.Title;
                    entity.Description = input.Description;
                    entity.KeyWords = input.KeyWords;
                    entity.CustomData = input.CustomData;
                    await _seoRepository.UpdateAsync(entity);
                }
                else
                {
                    entity = new Seo();
                    ObjectMapper.Map(input, entity);
                    await _seoRepository.InsertAsync(entity);
                }
            }
        }

        [AbpAuthorize(SeoPermissions.Pages_Manange_Tenant_Seo)]
        public async Task DeletAsync(MenuAndIdInput<Guid> input)
        {
            using (CurrentUnitOfWork.SetCultureName(AbpSession.CultureName()))
            {
                await _seoRepository.DeleteAsync(input.Id);
            }
               
        }

        [AbpAuthorize(SeoPermissions.Pages_Manange_Tenant_Seo)]
        public async Task DeleteByReleteIdAsync(MenuAndIdInput<Guid> input)
        {
            using (CurrentUnitOfWork.SetCultureName(AbpSession.CultureName()))
            {
                await _seoRepository.DeleteAsync(item => item.RelatedId == input.Id);
            }
        }

        public async Task<SeoDto> GetSeo(GetSeoInput input)
        {
            using (CurrentUnitOfWork.SetCultureName(AbpSession.CultureName()))
            {
                input.GetForEdit = true;
                return await _seoFrontEndAppService.GetSeo(input);
            }
            
        }

        [DisableAuditing]
        public SeoDto GetBackEndSeo()
        {
            return new SeoDto()
            {
                Title = L("ApplicationName")
            };
        }

    }
}
